package com.example.yls.valueanimator;

import android.animation.AnimatorInflater;
import android.animation.ValueAnimator;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private Button btnAnim;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnAnim=findViewById(R.id.btn_anim);
        btnAnim.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //利用代码实现属性动画(ofInt)
                //startPropertyAnimWithJava();
                //利用XML实现属性动画(ofInt)
                startPropertyAnimWithXML();
            }
        });
    }
    //利用XML实现属性动画(ofInt)
    private void startPropertyAnimWithXML() {
        ValueAnimator valueAnimator= (ValueAnimator) AnimatorInflater.loadAnimator
                (MainActivity.this,R.animator.set_animator);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int value = (int) valueAnimator.getAnimatedValue();
                Log.e(TAG,"value="+value);
                btnAnim.getLayoutParams().width=value;
                btnAnim.getLayoutParams().height=value;
                btnAnim.setText("属性动画"+value);
                btnAnim.setBackgroundColor(Color.argb(255,value/2,0,0));
                btnAnim.requestLayout();
            }
        });
        valueAnimator.start();
    }
    //利用代码实现属性动画(ofInt)
    private void startPropertyAnimWithJava() {
        //ValueAnimator valueAnimator = ValueAnimator.ofInt(0,300);
        ValueAnimator valueAnimator = ValueAnimator.ofInt
                (btnAnim.getLayoutParams().width,500);
        valueAnimator.setDuration(5000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int value = (int) valueAnimator.getAnimatedValue();
                Log.e(TAG,"value="+value);
                btnAnim.getLayoutParams().width=value;
                btnAnim.requestLayout();
            }
        });
        valueAnimator.setRepeatCount(5);
        valueAnimator.setRepeatMode(ValueAnimator.REVERSE);
        valueAnimator.start();
    }
}
